package ruc.utils;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.concurrent.ThreadLocalRandom;

public class ValueUtils {

    public static double getDoubleByRandom(int index) {
        int bound = index % 2 == 0 ? 100 : 10000;
        double d = ThreadLocalRandom.current().nextDouble(0, bound);
        d = (double) Math.round(d * 1000) / 1000;
        return d;
    }

    public static int getIntByRandom(int bound) {
        return ThreadLocalRandom.current().nextInt(0, bound);
    }

    public static boolean getBooleanByRandom() {
        return ThreadLocalRandom.current().nextBoolean();
    }

    public static long getLongByRandom() {
        return ThreadLocalRandom.current().nextLong(0, 1000000000000L);
    }

    //默认生成10位字母的字符串
    public static String getStringByRandom() {
        int length = 5;
        StringBuilder buffer = new StringBuilder("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
        StringBuilder sb = new StringBuilder();

        int range = buffer.length();
        for (int i = 0; i < length; i++) {
            sb.append(buffer.charAt(ThreadLocalRandom.current().nextInt(range)));
        }
        return sb.toString();
    }

    //默认生成1999-2022的随机一天日期
    public static Date getDateByRandom() {
        int startYear = 1999;                                    //指定随机日期开始年份
        int endYear = 2022;                                    //指定随机日期开始年份(含)
        long start = Timestamp.valueOf(startYear + 1 + "-1-1 0:0:0").getTime();
        long end = Timestamp.valueOf(endYear + "-1-1 0:0:0").getTime();
        long ms = (long) ((end - start) * ThreadLocalRandom.current().nextDouble() + start);    //获得了符合条件的13位毫秒数
        return new Date(ms);
    }
}
